package com.qc.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.sys.common.Constant;
import com.qc.sys.common.DataGridView;
import com.qc.sys.common.ResultObj;
import com.qc.sys.dao.SysRoleDao;
import com.qc.sys.entity.SysNotice;
import com.qc.sys.entity.SysRole;
import com.qc.sys.service.SysRoleService;
import com.qc.sys.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 * (SysRole)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-09-02 10:55:32
 */
@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleDao roleDao;

    @Override
    public DataGridView queryAllRole(RoleVo roleVo) {
        IPage<SysRole> page=new Page<>(roleVo.getPage(),roleVo.getLimit());
        QueryWrapper<SysRole> qw=new QueryWrapper<>();
        qw.eq(roleVo.getAvailable()!=null,"available",roleVo.getAvailable());
        qw.like(StringUtils.isNotBlank(roleVo.getName()),"name",roleVo.getName());
        qw.like(StringUtils.isNotBlank(roleVo.getRemark()),"remark",roleVo.getRemark());
        this.roleDao.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public ResultObj InsertRole(SysRole role) {
        try {
            role.setAvailable(Constant.USER_AVAILABLE_TRUE);
            role.setCreatetime(new Date());
            this.roleDao.insert(role);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    @Override
    public ResultObj UpdateRole(SysRole role) {
        try {
            this.roleDao.updateById(role);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 根据角色id查询角色有的菜单和权限id
     * */
    @Override
    public List<Integer> queryMenuIdsByRid(Integer id) {
        return this.roleDao.queryMenuIdsByRid(id);
    }

    @Override
    public void saveRoleMenu(Integer rid, Integer[] mids) {
        //先删除后添加
        this.roleDao.deleteRoleMenuByRid(rid);
        if (null!=mids&&mids.length>0){
            for (Integer mid : mids) {
                this.roleDao.insertRoleMenu(rid,mid);
            }
        }
    }

    @Override
    public DataGridView queryAllAvailableRoleNoPage(RoleVo roleVo) {
        QueryWrapper<SysRole> qw=new QueryWrapper<>();
        qw.eq(roleVo.getAvailable()!=null,"available",roleVo.getAvailable());
        List<SysRole> roles = this.roleDao.selectList(qw);
        //根据用户id查询已拥有的角色id
        List<Integer> roleIds = this.roleDao.queryRoleIdsByRid(roleVo.getUserId());
        List<Map<String,Object>> lists = new ArrayList<>();
        for (SysRole role : roles) {
            Boolean LAY_CHECKED = false;
            for (Integer roleId : roleIds) {
                if (role.getId().equals(roleId)){
                    LAY_CHECKED = true;
                    break;
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("id",role.getId());
            map.put("name",role.getName());
            map.put("remark",role.getRemark());
            map.put("LAY_CHECKED",LAY_CHECKED);
            lists.add(map);
        }
        return new DataGridView(Long.valueOf(lists.size()),lists);

    }


    @Override
    public boolean removeById(Serializable id) {
        //根据角色id删除角色和菜单之间的关系
        roleDao.deleteRoleMenuByRid(id);
        //根据角色id删除角色和用户之间的关系
        roleDao.deleteRoleUserByRid(id);
        return super.removeById(id);
    }

    @Override
    public List<String> queryRoleNamesByUserId(Integer id) {
        //根据用户ID查询角色ID的集合
        List<Integer> roleIds=this.roleDao.queryRoleIdsByUserId(id);
        if(null!=roleIds&&roleIds.size()>0){
            QueryWrapper<SysRole> qw=new QueryWrapper<>();
            qw.eq("available", Constant.USER_AVAILABLE_TRUE);
            qw.in("id",roleIds);
            List<SysRole> rolesObject=this.roleDao.selectList(qw);
            List<String> roles=new ArrayList<>();
            for (SysRole role : rolesObject) {
                roles.add(role.getName());
            }
            return roles;
        }else {
            return null;
        }
    }
}